/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.clinicplus.manager.search;

import br.com.foxline.clinicplus.entity.*;
import br.com.foxline.clinicplus.enums.CivilStatus;
import br.com.foxline.clinicplus.enums.Gender;
import br.com.foxline.clinicplus.service.AnnexService;
import br.com.foxline.clinicplus.service.CustomerService;
import br.com.foxline.clinicplus.util.Caracter;
import br.com.foxline.clinicplus.util.Msg;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CaptureEvent;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerSearchCustomer implements Serializable {

    @EJB
    private CustomerService customerService;
    @EJB
    private AnnexService annexService;
    private Customer customer;
    private Customer aux;
    private Customer customerShow;
    private Operator operator;
    private Operator operatorShow;
    private Address address;
    private Address addressShow;
    private City city;
    private City cityShow;
    private List<Customer> customers;
    private List<Customer> searchCustomers;
    private Annex annex;
    private Annex annexShow;
    private boolean back;
    private boolean voltarAtendimentoPerfil;
    private boolean voltarAtendimentoProcedimento;
    private String dateString;

    @PostConstruct
    public void init() {

        customer = new Customer();


        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        String edit = params.get("customerEdit");
        String show = params.get("customerShow");


        back = params.get("action") == null ? false : true;
        voltarAtendimentoPerfil = params.get("atendimentoperfil") == null ? false : (Boolean.parseBoolean(params.get("atendimentoperfil"))) ;
        voltarAtendimentoProcedimento = params.get("atendimentoprocedimento") == null ? false : (Boolean.parseBoolean(params.get("atendimentoprocedimento")));

        if (edit != null && !edit.equals("")) {
            this.customer = customerService.find(Long.parseLong(edit));
            this.address = customer.getAddress();
            this.annex = customer.getAnnex();
            if (this.annex == null) {
                this.annex = annex;
            }
            if (this.address == null) {
                this.address = new Address();
                this.city = new City();
            } else {
                this.city = address.getCity();
            }

            this.operator = customer.getOperator();
        } else if (show != null) {
            this.customerShow = customerService.find(Long.parseLong(show));
            this.addressShow = customerShow.getAddress();
            this.annexShow = new Annex();

            if (this.annexShow != null) {
                this.annexShow = customerShow.getAnnex();
            }

            if (this.addressShow != null) {
                this.cityShow = this.addressShow.getCity();
            }
            this.operatorShow = this.customerShow.getOperator();
        } else {
            this.customers = new ArrayList<Customer>();
            //this.customers = customerService.findAll();
        }
    }

    public void searchCustomer() {
        customer.setCpf(Caracter.removecaracter(customer.getCpf()));
        searchCustomers = customerService.findCustomer(customer);
    }

    public Customer getCustomerShow() {
        return customerShow;
    }

    public Address getAddressShow() {
        return addressShow;
    }

    public void setAddressShow(Address addressShow) {
        this.addressShow = addressShow;
    }

    public City getCityShow() {
        return cityShow;
    }

    public void setCityShow(City cityShow) {
        this.cityShow = cityShow;
    }

    public Operator getOperatorShow() {
        return operatorShow;
    }

    public void setOperatorShow(Operator operatorShow) {
        this.operatorShow = operatorShow;
    }

    public void setCustomerShow(Customer customerShow) {
        this.customerShow = customerShow;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }

    public Operator getOperator() {
        return operator;
    }

    public void setOperator(Operator operator) {
        this.operator = operator;
    }

    public List<Customer> autocomplete(String query) {
        return customerService.autocomplete(query);
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public List<Customer> getSearchCustomers() {
        return searchCustomers;
    }

    public void setSearchCustomers(List<Customer> searchCustomers) {
        this.searchCustomers = searchCustomers;
    }

    public List<SelectItem> getGender() {
        List<SelectItem> genders = new ArrayList<SelectItem>();
        for (Gender ms : Gender.values()) {
            genders.add(new SelectItem(ms, ms.getName()));
        }
        return genders;
    }

    public List<SelectItem> selectCivilStatus() {
        List<SelectItem> civilStatus = new ArrayList<SelectItem>();
        for (CivilStatus ms : CivilStatus.values()) {
            civilStatus.add(new SelectItem(ms, ms.getName()));
        }
        return civilStatus;
    }

    public void update() {
        try {
            address.setCity(city);
            customer.setAddress(address);

            if (annex != null && annex.getId() != null) {
                customer.setAnnex(annex);
            }
            customer.setOperator(operator);
            customerService.update(customer);
        }catch(Exception e){
            e.printStackTrace();
            Msg.messagemError();
            return;
        }
        if (voltarAtendimentoPerfil) {
            Msg.messagemInfoRedirect("Usuário Atualizado", "atendimentoperfil.xhtml?step=2&customer=" 
                    + customer.getId()+"&date="+dateString);
        } else if (voltarAtendimentoProcedimento) {
            Msg.messagemInfoRedirect("Usuário Atualizado", "atendimentoprocedimento.xhtml?step=3&customer="
                    + customer.getId()+"&date="+dateString);
        } else {
            Msg.messagemInfo("Paciente atualizado com Sucesso");
        }
        if (back) {
            redirect("agenda.xhtml");
        }

    }

    public void redirect(String string) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
        }
    }

    public void oncapture(CaptureEvent captureEvent) {
        System.err.println("Editando foto");

        annex = new Annex();
        annex.setDescription("AVATAR");
        annex.setFileByte(captureEvent.getData());
        annex.setType("image/jpeg");

        annexService.save(annex);
    }

    public Annex getAnnex() {
        return annex;
    }

    public void setAnnex(Annex annex) {
        this.annex = annex;
    }

    public Annex getAnnexShow() {
        return annexShow;
    }

    public void setAnnexShow(Annex annexShow) {
        this.annexShow = annexShow;
    }

    public Customer getAux() {
        return aux;
    }

    public void setAux(Customer aux) {
        this.aux = aux;
    }

    public boolean isBack() {
        return back;
    }

    public void setBack(boolean back) {
        this.back = back;
    }

    public boolean exist() {

        aux = customerService.existCustomer(customer);
        if (aux != null && (aux.getId() != customer.getId())) {
            RequestContext context = RequestContext.getCurrentInstance();
            context.update(":customerEdit:customerDlg");
            context.update(":customerEdit:yes");
            context.execute("customerDlg.show();");
            return true;
        }
        return false;
    }

    public void loadCustomer() {
        customer = aux;
    }

    public void clearCpf() {
        customer.setCpf("");
        aux = null;
    }

    public boolean isVoltarAtendimentoPerfil() {
        return voltarAtendimentoPerfil;
    }

    public void setVoltarAtendimentoPerfil(boolean voltarAtendimentoPerfil) {
        this.voltarAtendimentoPerfil = voltarAtendimentoPerfil;
    }

    public boolean isVoltarAtendimentoProcedimento() {
        return voltarAtendimentoProcedimento;
    }

    public void setVoltarAtendimentoProcedimento(boolean voltarAtendimentoProcedimento) {
        this.voltarAtendimentoProcedimento = voltarAtendimentoProcedimento;
    }

    public String getDateString() {
        return dateString;
    }

    public void setDateString(String dateString) {
        this.dateString = dateString;
    }
    
    
}
